//客服聊天
import UIKit

class TF_livingTableView: UIView , UITableViewDelegate , UITableViewDataSource {
    
    @objc public let list = NSMutableArray()
    @objc public let tableView = UITableView()
    @objc public var xid:NSString = ""
    
    override init(frame: CGRect) {
        super.init(frame: frame)
   
        self.tableView.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
        self.addSubview(self.tableView)
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.separatorStyle = .none;
        self.tableView.separatorInset = .zero;
        self.tableView.register(TF_LivingTableViewCell.self, forCellReuseIdentifier: "TF_LivingTableViewCell")
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.list.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TF_LivingTableViewCell", for: indexPath) as! TF_LivingTableViewCell
        cell.selectionStyle = .none
        
        let model = self.list[indexPath.row] as! TFChatData
      
        cell.backgroundColor = UIColor.clear
    

        cell.viewWidth = self.frame.size.width
       
        cell.viewHeight = self.tableView(tableView, heightForRowAt: indexPath)

        cell.model = model;
       
        return cell
    }
    // 设置cell高度
       func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
           
           let model = self.list[indexPath.row] as! TFChatData
           let font = UIFont.PingFang(size: 15)
           
           let string = TalkfunSharedTools.getAttributedText(model.msg, fontSize: font, foregroundColor: UIColor.white)
           let size = TalkfunSharedTools.getSizeLabel(labelString: string, labelWidth:self.frame.size.width-62-28, labelfont: font)
           
           return size.height+42+6+7+10
           
          
       }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    
        self.tableView.frame = self.bounds;
    }
    //添加聊天列表数据
    @objc public func addChatList(list:NSMutableArray)

    {
        self.list.removeAllObjects()
       
//        if (lis.count) {
        self.list.addObjects(from: list as! [Any])

//        }
        self.tableView.reloadData()
       
        
        if (self.list.count > 0) {
            let indexpath = NSIndexPath.init(row: self.list.count - 1, section: 0)
            self.tableView.scrollToRow(at: indexpath as IndexPath, at: .bottom, animated: true)
            
//            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.array.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        }
    }

    @objc public func addChatUI2(model:TFChatData)
    //添加聊天数据

    {
        if ((model) == nil) {

        }else{
            self.list.add(model)
//            [self.array addObject:model];
//            [self.tableView reloadData];
            self.tableView.reloadData()
        }

    }
    
}
